<?php

if (!defined('BASEPATH'))
exit('No direct script access allowed');

class Projectdetail extends MX_Controller {
	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 * 	- or -
	 * 		http://example.com/index.php/welcome/index
	 * 	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	function Projectdetail() {
		parent::__construct();
		$this->load->model('lookupmodel');
		$this->load->model('helpermodel');
		//check is logged in
		$this->load->model('languagemodel');
		$this->load->model('projects/mdl_projectsmodel');
		$this->language = $this->languagemodel->is_language_using();
		$this->load->helper('MY_url_helper');
		$this->load->helper('text');
		$this->load->helper('utf8');
		$this->load->model('imagemodel');
		
		$this->load->model('banners/bannermodel');
	}
	public function index($url) {
		$this->lang->load('common/header', $this->language);
		$this->lang->load('projects/projects', $this->language);

		//SEO
		$data['title'] = $this->lang->line('text_project_title');
		$data['description'] = $this->lang->line('text_project_description');
		$data['keywords'] = $this->lang->line('text_project_keywords');

		$data['breadcrumbs'] = array();

		$data['breadcrumbs'][] = array(
			'text'      => $this->lang->line('text_home'),
			'href'      => base_url()
		);

		$data['breadcrumbs'][] = array(
			'text'      => $this->lang->line('text_heading'),
			'href'      => base_url() . 'projects.html'
		);
		
		$parts = explode('-', $url);
        $id = end($parts);

		//$id = $this->input->get('id');

		if(!isset($id) || !is_numeric($id)) {
			redirect(base_url() . 'error_404', 'refresh');
		}

		$options['listing_id'] = $id;

		if ($this->input->get('page')) {
			$options['page'] = $this->input->get('page');
		} else {
			$options['page'] = 1;
		}
		
		$options['localorint'] = NULL;
		$options['offset'] = NULL;
		$options['limit'] = NULL;
		$options['language'] = NULL;
		$options['market'] = NULL;
		$options['country'] = NULL;
		$options['propertytype'] = NULL;
		$options['cities'] = NULL;
		$options['locations'] = NULL;
		$options['subarea'] = NULL;
		$options['size_min'] = NULL;
		$options['size_max'] = NULL;
		$options['keyword'] = NULL;
		$options['orderby'] = "UpdateDate DESC";

		$results = $this->mdl_projectsmodel->getProjectDirectory($options);

		if(isset($results) && is_array($results)) {
			$data['listingtoview'] = $results[0];
		} else {
			redirect(base_url() . 'error_404', 'refresh');
		}

		$data['breadcrumbs'][] = array(
			'text'      => $data['listingtoview']['ProjectName'],
			'href'      => base_url() . 'project-detail/'. $url
		);

		//get Type of Unit by Project Directory ID
		$data['TypeOfUnits'] = array();
		$results = $this->mdl_projectsmodel->getTypeOfUnit($id);
		if($results) {
			foreach ($results as $result) {
				$data['TypeOfUnits'][] = array(
				'Name'	=>	$result->Name
				);
			}
		}

		//get Tenure by Project Directory Id
		$data['Tenure'] = "";
		$result = $this->mdl_projectsmodel->getTenure($id, $this->config->item('config_language_id'));
		
		if($result) {
			$data['Tenure'] = $result->Name;
		}

		//get Mode Type by Project Directory Id
		$results = $this->mdl_projectsmodel->getModeType($id, $this->config->item('config_language_id'), 'FloorPlan');
		$data['modetype'] = array();
		if($results) {
			foreach ($results as $result) {
				if(isset($result->FileName) && isset($result->FileExtension)) {
					$image = $this->config->item('HTTP_PRODIR_MEDIA_IMAGE') . $result->PrjDirectoryFID . '/' . $result->FileName . $result->FileExtension;
				} else {
					$image = $this->config->item('HTTP_IMAGE') . 'tmp/' . 'no-image.png';
				}
				$data['modetype'][] = array(
					'FloorPlan_ID'		=> $result->FloorPlan_ID,
					'PrjDirectoryFID'	=> $result->PrjDirectoryFID,
					'ModelType'			=> $result->ModelType,
					'NoUnit'			=> $result->NoUnit,
					'Description'		=> $result->Description,
					'FileName'			=> $result->FileName,
					'FileExtension'		=> $result->FileExtension,
					'href'				=> $image
				);
			}
		}

		//get Agent Info
		$this->load->model('member/mdl_member');
		$this->load->model('findanexpert/mdl_findanexpert');
		
		$data['agentInfo'] = array();
		$results = $this->mdl_member->getAgentInfo2($id, $this->config->item('config_language_id'),$this->config->item('market'));
		
		if($results) {
			//$agentPhoto = $this->mdl_findanexpert->getAgentPhoto($user_id);
			foreach ($results as $result) {
				//$agentPhoto = false;
				//$agentPhoto = $this->mdl_findanexpert->getAgentPhoto($result->User_ID);
				$agentPhoto = false;
				if(isset($result->Photo)) {
					$agentPhoto = true;
				}
				
				$data['agentInfo'][] = array(
					'Agent_ID'		=> $result->User_ID,
					'Name'			=> $result->BusinessName,
					'Mobile'		=> $result->Mobile,
					'Email'			=> $result->Email,
					'AgentPhoto'	=> $agentPhoto
				);
			}
		} else {
			$data['agentInfo'] = false;
		}
		//get similar Project Directory
		$data['similar_listings'] = array();
		
		$results = $this->mdl_projectsmodel->getProductRelated($id);
		
		foreach ($results as $result) {
			$listingtoview['media'] = $this->mdl_projectsmodel->get_media($result->ID, "Gallery", 1);
			if($listingtoview['media']) {
				$image = $this->config->item('HTTP_PRODIR_MEDIA_IMAGE') . $result->ID . '/' . str_replace(' ', '%20', $listingtoview['media']->FileName . '-' . THUMB_IMAGE_WIDTH . 'x' .THUMB_IMAGE_HEIGHT . $listingtoview['media']->FileExtension);	
			} else {
				$image = false;
			}
			
			$location = "";
			if(!empty($result->City)) {
				$location .= $result->City;
			}
			
			if(!empty($result->Location)) {
				$location .= ', ' . $result->Location;
			}
			
			if(!empty($result->Subarea)) {
				$location .= ', ' . $result->Subarea;
			}
			
			$data['similar_listings'][] = array(
				'listing_id'	=> $result->ID,
				'thumb'			=> $image,
				'name'			=> $result->ProjectName,
				'location'		=> $location,
				'Link'			=> $result->Link
			);
		}
		
		//get banner for right side
		$data['banner'] = $this->bannermodel->getBanners('Result', $this->config->item('market'), $this->config->item('config_language_id'));

		$data['module'] = "projectdetail";
		$data['view_file'] = "projectdetail";

		echo Modules::run('template/default_template', $data);
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */